using SmartLink.EnumLibrary;
using SmartLink.RedisRepository;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Infrastructure.Cache
{
    public class OrderCaches
    {
        /// <summary>
        /// 构成  时间+业务id+顺序号
        /// </summary>
        private static ConcurrentDictionary<string, int> DicCurDaySequenceNum = new ConcurrentDictionary<string, int>();
        private static object obj = new object();

        /// <summary>
        /// 获取订单编号
        /// </summary>
        /// <param name="redisKey">订单类型缩写 如：HTOrderNum（合同订单编号）</param>
        /// <returns></returns>
        public static string GetOrderNumCache(string redisKey)
        {
            string OrderNum = RedisManager.GetInstance().QueueGet(redisKey);
            return string.IsNullOrEmpty(OrderNum) ? "" : OrderNum;
        }


        public static string GetOrderNumCache(CodeTypeEnum orderTypeNum)
        {
            string dayTimeNow = DateTime.Now.ToString("yyyyMMddHHmmss");
            string dayTime = DateTime.Now.ToString("yyyy");
            string workCode = EnumHelper.GetDesription(orderTypeNum);
            string key = string.Format("{0}_{1}", workCode, dayTime);
            int sequenceNum = 1;
            string redisKey = "OneDaySequence";
            string hashKey = string.Format("{0}", workCode);

            lock (obj)
            {
                string curSequenceNum = RedisManager.GetInstance().HashGet(redisKey, hashKey);
                //redis未获取到
                if (!string.IsNullOrEmpty(curSequenceNum))
                {
                    sequenceNum = Convert.ToInt32(curSequenceNum);
                    sequenceNum++;
                }
                RedisManager.GetInstance().HashSet(redisKey, hashKey, sequenceNum.ToString());
            }

            return string.Format("{0}{1}{2}", workCode, dayTimeNow, sequenceNum.ToString("000000"));
        }

    }
}
